<template>
  <div class="mod-study__event">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item label="模糊搜索">
        <el-input v-model="state.dataForm.keywords" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-tabs v-model="state.dataForm.status" class="demo-tabs" @tab-change="state.getDataList()">
      <el-tab-pane :label="item.label" :name="item.value" v-for="(item, index) in commentStatusList" :key="index" />
    </el-tabs>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
      <el-table-column prop="grade" label="评价配图" header-align="center">
        <template v-slot="scope">
          <div style="display: flex;justify-content: center;">
            <el-image style="width: 100px; height: 100px" v-for="item in scope.row.visualsList" :key="item.id" :src="item.url" fit="contain" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="grade" label="评价等级" header-align="center">
        <template v-slot="scope">
          <el-rate v-model="scope.row.grade" disabled text-color="#ff9900" />
        </template>
      </el-table-column>
      <el-table-column prop="content" label="评价内容" header-align="center"></el-table-column>
      <el-table-column prop="creatorName" label="纪实人" header-align="center"></el-table-column>
      <el-table-column prop="eventName" label="研学名称" header-align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" v-if="state.dataForm.status === 'NOT_FEATURED'">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('study:comment:update')" type="primary" link
            @click="addOrUpdateHandle(scope.row.id, 'FEATURED')">精选</el-button>
          <el-button v-if="state.hasPermission('study:comment:update')" type="primary" link
            @click="addOrUpdateHandle(scope.row.id, 'NOT_PUBLISHED')">不发布</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs } from "vue";
import useView from "@/hooks/useView";
import { commentStatusList } from "@/constants/config";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import baseService from "@/service/baseService";

const router = useRouter();
const view = reactive({
  getDataListURL: "/study/comment/page",
  getDataListIsPage: true,
  deleteURL: "/study/comment",
  deleteIsBatch: true,
  dataForm: {
    keywords: "",
    status: "NOT_FEATURED",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateHandle = (id?: string, status?: string) => {
  ElMessageBox.confirm(
    '确定精选?',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      baseService.put("/study/comment/update/status", { id, status }).then((res) => {
        ElMessage.success({
          message: "成功",
          duration: 500,
          onClose: () => {
            state.getDataList();
          }
        });
      });
    })
};
</script>
